package eng.jTrussSolver.Rod;

import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RodHashMap extends HashMap<Integer, RodData> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int _RodCounter ;
	
	public RodHashMap()
	{
		_RodCounter=0;
	}
	

	
	/**
	 * adds rods at the end of rods list
	 * @param Rod
	 */
	public void addRod(RodData rodData) {
		this.put(_RodCounter, rodData);
		_RodCounter =_RodCounter+1;
	}

	public void setRod(RodData rodData) {
		this.remove(rodData.RodID);
		this.put(rodData.RodID, rodData);
	}
	
	public int getRodsHashKey() {
		return _RodCounter;
	}
	
	public int getIDofRodName(String RodName) {
		int RodID = 0;
		for (Integer Key : this.keySet()) {
			String HashKeyName = this.get(Key).Name;
			if (HashKeyName.equals(RodName)) {
				RodID = Key;
			}
		}
		return RodID;
	}
	
	/**
	 * Code For Rods Data
	 * @param doc
	 * @return
	 */
	public Element prepareXMLelement(Document doc, HashMap<Integer, RodData> rods) {
		
		Element B = doc.createElement("Rods");

		for (Integer i : rods.keySet()) {

			Element RodElement = doc.createElement("Rod");
			B.appendChild(RodElement);

			Element g = doc.createElement("RodID");
			g.appendChild(doc.createTextNode(String.valueOf((rods.get(i).RodID))));
			RodElement.appendChild(g);

			Element el = doc.createElement("Name");
			el.appendChild(doc.createTextNode(rods.get(i).Name));
			RodElement.appendChild(el);

			Element sn = doc.createElement("Start_Node");
			sn.appendChild(doc.createTextNode(String.valueOf(rods.get(i).NodeStart)));
			RodElement.appendChild(sn);

			Element en = doc.createElement("End_Node");
			en.appendChild(doc.createTextNode(String.valueOf(rods.get(i).NodeEnd)));
			RodElement.appendChild(en);

			Element m = doc.createElement("RodMaterial");
			m.appendChild(doc.createTextNode(String.valueOf(rods.get(i).RodMaterial)));
			RodElement.appendChild(m);

			Element s = doc.createElement("RodSection");
			s.appendChild(doc.createTextNode(String.valueOf(rods.get(i).RodSection)));
			RodElement.appendChild(s);

		}
		return B;
	}

	/**
	 * Clears() Hashmap and also resets counter.
	 */
	public void clear()
	{
		super.clear();
		this._RodCounter =0;
	}

}
